import {
  login
} from '../../network/interface';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    isLogin:false,
    hasUserInfo: false,
  },
  getUserProfile: function (e) {
    console.log(11111111)
    var that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
              that.setData({
                userInfo: res.userInfo,
              })
              wx.login({
                success: res => {
                  console.log("用户的code:" + res.code);
                  wx.request({
                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx6027f2aadbd672db&secret=95e2fb6a20599ba6c3d3bb899f05266e&js_code=' + res.code + '&grant_type=authorization_code',
                    success: res => {
                      // 获取到用户的 openid
                      console.log("用户的openid:" + res.data.openid);
                      let params = {}
                      params.openId = res.data.openid
                      params.nickname = that.data.userInfo.nickName
                      params.wxAvatar = that.data.userInfo.avatarUrl
                      params.gender = that.data.userInfo.gender
                      login(params, function success(res) {
                        wx.setStorage({
                          key:"token",
                          data:res.data
                        })
                        console.log("登录成功")
                        wx.switchTab({
                          url: '/pages/foodMain/foodMain'
                        })  

                       
                      }, function fail(res) {
                        wx.showToast({
                          title: '授权失败!',
                          icon: 'none'
                        })
                      })

                    }
                  });

                }
              })
            }
          })
        }
      }
    })
  },


  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    var that =this;
    console.log(e)
    console.log(11111111)
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo)
        this.setData({
          userInfo: res.userInfo
        })
        wx.login({
          success: res => {
            console.log("用户的code:" + res.code);
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx6027f2aadbd672db&secret=95e2fb6a20599ba6c3d3bb899f05266e&js_code=' + res.code + '&grant_type=authorization_code',
              success: res => {
                // 获取到用户的 openid
                console.log("用户的openid:" + res.data.openid);
                wx.showLoading({
                  title: '登录中',
                  icon:'none'
                })
                let params = {}
                params.openId = res.data.openid
                params.nickname = that.data.userInfo.nickName
                params.wxAvatar = that.data.userInfo.avatarUrl
                params.gender = that.data.userInfo.gender
                wx.setStorage({
                  key:"userInfo",
                  data:params
                })
                login(params, function success(res) {
                  if(res['status']===200){
                    wx.setStorage({
                      key:"token",
                      data:res.data
                    })
                    wx.hideLoading({
                      success: (res) => {},
                    })
                    wx.switchTab({
                      url: '/pages/foodMain/foodMain'
                    })  
                  }
                
    
                 
                }, function fail(res) {
                  wx.showToast({
                    title: '授权失败!',
                    icon: 'none'
                  })
                })
    
              }
            });
    
          }
        })
    
      }
    })

   

  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})